package dw.fastfile.beetl.service;

import dw.fastfile.business.dto.Dog;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.StringTemplateResourceLoader;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: wanghao
 * @create: 2023-10-16 14:56
 **/
public class BeetlUtils {
    public static void main(String[] args) throws IOException {
        String name = "小王";
        Dog user = new Dog();
        user.setName("狗狗");

        Map<String, Object> dataMap = new HashMap<>();
        List<Map<String, Object>> list = new ArrayList<>();

        Map<String, Object> item = new HashMap<>();
        item.put("sseTrackingNumber", "编号1");
        item.put("paymentLegalEntity", "实体1");
        item.put("billType", "单据类型1");
        item.put("documentDate", "支付日期1");
        item.put("dateOfApplication", "日期1");
        item.put("status", "状态1");
        list.add(item);

        dataMap.put("title", "标题1");
        dataMap.put("endDate", "结束日期");

        dataMap.put("dataList", list);
        String result = getResult(getT(), dataMap);
        System.out.println(result);

    }

    private static String getResult(String templateStr, Map<String, Object> dataMap) throws IOException {
        //初始化代码
        StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
        Configuration cfg = Configuration.defaultConfiguration();
        GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
        // 获取模板
        Template template = gt.getTemplate(templateStr);
        template.binding(dataMap);
        // 渲染结果
        String str = template.render();
        return str;
    }

    private static String getT() {
        String s = "<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>${title}</title>\n" +
                "</head>\n" +
                "<style>\n" +
                " body{\n" +
                "  width:1211px;\n" +
                "  margin:0 auto;\n" +
                "  display: flex;\n" +
                "  align-items: center;\n" +
                "  justify-content: center;\n" +
                " }\n" +
                " .mycontainer{\n" +
                "    height: 550px;\n" +
                "    width: 1200px;\n" +
                "    display: flex;\n" +
                "    align-items: center;   \n" +
                "    justify-content: center;\n" +
                "    overflow:auto;\t\t\n" +
                "    }\n" +
                "    table{\n" +
                "        width:900px;\n" +
                "        background-color:#f1f5fe;\n" +
                "        border-radius:20px;\n" +
                "        text-align:center;\n" +
                "        color:#1C5FB6;\n" +
                "        border-collapse:collapse;\n" +
                "        margin:10px 0 0 10px;\n" +
                "    }\n" +
                "    table td{\n" +
                "        border-top:0;\n" +
                "        border-right:1.5px solid #e5e9f4;\n" +
                "        border-bottom:1.5px solid #e5e9f4;\n" +
                "        border-left:0;\n" +
                "    }\n" +
                "    table tr{\n" +
                "        height:auto;\n" +
                "        font-size:16px;\n" +
                "    }\n" +
                "    table tr:last-child td{\n" +
                "        border-bottom:0;\n" +
                "    }\n" +
                "    table tr td.lastCol{\n" +
                "         border-right:0;\n" +
                "    }\n" +
                "</style>\n" +
                "<body>\n" +
                "<div>\n" +
                "    <seg class=\"fBold\">您好：财务实付金额已返回，请在${endDate} 日前确认实付金额并在【付款申请单】中完成支付“发送”操作！</seg>\n" +
                "</div>\n" +
                "<div class=\"mycontainer\">\n" +
                "    <table class=\"table\">\n" +
                "        <tr style=\"font-weight:600;\">\n" +
                "            <td style=\"width:140px;\">编码</td>\n" +
                "           <td style=\"width:140px;\">费用支付法人实体</td>\n" +
                "            <td style=\"width:140px;\">款项类型</td>\n" +
                "\t    <td style=\"width:140px;\">财务实际支付日期</td>\n" +
                "\t    <td style=\"width:140px;\">申请日期</td>\n" +
                "            <td style=\"width:160px;\">支付状态</td>\n" +
                "        </tr>\n" +
                "  <%\n" +
                "    var list = dataList;\n" +
                "    for(item in list){\n" +
                "        println(\"<tr><td> \"+item.sseTrackingNumber+ \" </td><td> \"+item.paymentLegalEntity+ \" </td><td> \"+item.billType+ \" </td><td> \"+item.documentDate+ \" </td><td> \"+item.dateOfApplication+ \" </td><td> \"+item.status+ \" </td></tr>\");\n" +
                "    }\n" +
                "  %>\n" +
                "    </table>\n" +
                "\n" +
                "</div>\n" +
                "</body>\n" +
                "</html>";
        return s;
    }
}
